package com.ple.app.importdata;

import android.app.DatePickerDialog;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.ple.app.jzsf.R;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Map;

public class Cultivateinfo_Activity extends Activity {
    private EditText addcultivate_crop;
    private EditText addcultivate_irri;
    private EditText addculitivate_manure;
    private EditText addcultivate_seed;
    private EditText addcultivate_data;
    private EditText addcultivate_total;
    private EditText addcultivate_dis;
    private EditText addcultivate_deep;
    private EditText addcultivate_yield;
    private EditText addculitivateirri_numble;
    private TextView save_modify;
    private ImageButton addirr_times,addmanure_times;
    private LinearLayout linearLayout1,linearLayout2;
    private DatePickerDialog datePickerDialog;
    private  final String[] culitivate_conlm={"ID","crop","seed","date","total" ,"distance","deep","yield",
            "how_irr","irr_number","manure_number"};
    private String id;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cultivateinfo_);
        addcultivate_crop= (EditText) findViewById(R.id.addcultivate_cropi);
        addcultivate_irri= (EditText) findViewById(R.id.addcultivate_irrii);
        addculitivate_manure= (EditText) findViewById(R.id.addculitivate_manurei);
        addcultivate_seed= (EditText) findViewById(R.id.addcultivate_seedi);
        addcultivate_data=(EditText)findViewById(R.id.addcultivate_datai);
        addcultivate_total=(EditText)findViewById(R.id.addcultivate_totali);
        addcultivate_dis=(EditText)findViewById(R.id.addcultivate_disi);
        addcultivate_deep=(EditText)findViewById(R.id.addcultivate_deepi);
        addcultivate_yield=(EditText)findViewById(R.id.addcultivate_yieldi);
        addculitivateirri_numble= (EditText) findViewById(R.id.irrinumble);
        addirr_times= (ImageButton) findViewById(R.id.iadd_manuretimes);
        addmanure_times= (ImageButton) findViewById(R.id.iadd_times);
        linearLayout1= (LinearLayout) findViewById(R.id.numble1);
        linearLayout2= (LinearLayout) findViewById(R.id.numble2);
        save_modify= (TextView) findViewById(R.id.modify);
        /**
         * 添加灌溉次数
         */
        addirr_times.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int i =1+Integer.valueOf(addculitivateirri_numble.getText().toString());
                String string=String.valueOf(i);
                String times="第"+string+"次灌溉时间";
                addculitivateirri_numble.setText(string);
                addLinearLayout(linearLayout1,times,null);
            }
        });
        /**
         * 施肥次数
         */
        addmanure_times.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int i =1+Integer.valueOf(addculitivate_manure.getText().toString());
                String string=String.valueOf(i);
                String times="第"+string+"次施肥时间";
                addculitivate_manure.setText(string);
                addLinearLayout(linearLayout2,times,null);
            }
        });

        Intent intent=getIntent();
        Bundle bundle=intent.getExtras();
         id=bundle.getString("id");

        getinformation(id);//设置文本框显示值
       /**
       * 保存修改
       */
        save_modify.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String[] args=new String[]{id};
                MyopenHelper myopenhelper=new MyopenHelper(Cultivateinfo_Activity.this,"seed.db",null,1);
                SQLiteDatabase db=myopenhelper.getReadableDatabase();
                ContentValues values=new ContentValues();
                values.put("irr_number",addculitivateirri_numble.getText().toString());
                values.put("manure_number",addculitivate_manure.getText().toString());
                db.update("cultivate",values,"ID=?",args);
                db.close();
                Cultivateinfo_Activity.this.finish();
                Toast.makeText(getApplicationContext(),"数据修改成功！",Toast.LENGTH_LONG).show();
            }
        });


    }

    /**
     *
     * @param id
     */
    private void getinformation(String id){
       // Log.i("aaa", "getinformation: "+id);
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        String[] args=new String[]{id};
        MyopenHelper myopenhelper=new MyopenHelper(Cultivateinfo_Activity.this,"seed.db",null,1);
        SQLiteDatabase db=myopenhelper.getReadableDatabase();
        Cursor curor=db.query("cultivate",culitivate_conlm,"ID=?",args,null,null,null,null);

        if(curor.moveToFirst()) {
            addcultivate_crop.setText(curor.getString(1));
            addcultivate_seed.setText(curor.getString(2));
            addcultivate_data.setText(curor.getString(3));
            addcultivate_total.setText(curor.getString(4));
            addcultivate_dis.setText(curor.getString(5));
            addcultivate_deep.setText(curor.getString(6));
            addcultivate_yield.setText(curor.getString(7));
            addcultivate_irri.setText(curor.getString(8));
            addculitivateirri_numble.setText(curor.getString(9));
            addculitivate_manure.setText(curor.getString(10));

            int irr_number = Integer.valueOf(curor.getString(9));
            if (irr_number > 0) irrigation(irr_number, id);
            int manure_numbemanm = Integer.valueOf(curor.getString(10));//获得施肥次数，建相应的显示框
            if (manure_numbemanm > 0) manure(manure_numbemanm, id);
        }
        curor.close();
        db.close();
    }
    /**
     *
     */
    /**
     * 添加控件
     */
    private void addLinearLayout(LinearLayout linearLayout, final String times, String datetext){
        LinearLayout layout=new LinearLayout(this);
        LinearLayout.LayoutParams Lparams=new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);
        LinearLayout.LayoutParams Tparams=new LinearLayout.LayoutParams(0,ViewGroup.LayoutParams.WRAP_CONTENT,1);
        layout.setLayoutParams(Lparams);
        layout.setPadding(0,2,0,0);
        layout.setOrientation(LinearLayout.HORIZONTAL);

        TextView textView=new TextView(this);
        textView.setLayoutParams(Tparams);
        textView.setGravity(Gravity.CENTER);
        textView.setText(times);

        final EditText editText=new EditText(this);
        editText.setLayoutParams(Tparams);
        editText.setGravity(Gravity.CENTER);
        editText.setBackgroundColor(getResources().getColor(R.color.green3));
        editText.setFocusable(false);
        editText.setText(datetext);

        //设置点击事件
        date(editText,times);


        layout.addView(textView);
        layout.addView(editText);
        linearLayout.addView(layout);
    }
    /**
     * choose date
     */
    private void date(final EditText editText, final String times){
        editText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Calendar calendar=Calendar.getInstance();
                datePickerDialog=new DatePickerDialog(Cultivateinfo_Activity.this, new DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                        int month1=month+1;
                        String data_text = year + "-" + month1+ "-" + dayOfMonth;
                        editText.setText(data_text);
                        MyopenHelper myopenhelper = new MyopenHelper(Cultivateinfo_Activity.this, "seed.db", null, 1);
                        SQLiteDatabase db = myopenhelper.getReadableDatabase();
                        String[]args=new String[]{String.valueOf(id),times};
                        String[] cul_date=new String[]{"ID","operation","date"};
                        Cursor curor=db.query("cul_date",cul_date,"ID=? and operation=?",args,null,null,null,null);
                        ContentValues values = new ContentValues();
                        if(curor.getCount()>0) {
                            values.put("date", data_text);
                            db.update("cul_date",values,"ID=? and operation=?",args);
                        }
                        else{
                            values.put("ID",id);
                            values.put("operation", times);
                            values.put("date", data_text);
                            db.insert("cul_date", null, values);}
                        curor.close();
                        db.close();
                    }
                },calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH),calendar.get(Calendar.DAY_OF_MONTH));
                datePickerDialog.show();
            }
        });
    }
    /**
     *灌溉
     */
    private void irrigation(int numble,String id){

        String[] cul_date=new String[]{"ID","operation","date"};
        MyopenHelper myopenhelper = new MyopenHelper(Cultivateinfo_Activity.this, "seed.db", null, 1);
        SQLiteDatabase db = myopenhelper.getReadableDatabase();
         for(int i=1;i<=numble;i++){
             String string=String.valueOf(i);
             String times="第"+string+"次灌溉时间";
             String[]args=new String[]{id,times};
             Cursor curor=db.query("cul_date",cul_date,"ID=? and operation=?",args,null,null,null,null);
             //String string=String.valueOf(i);
             String datetext="...";
             if(curor.moveToFirst()){
             datetext=curor.getString(2);
             }
             curor.close();
             addLinearLayout(linearLayout1,times,datetext);

         }
        db.close();
    }
    /**
     *施肥
     */
    private void manure(int numble,String id){
        String[] cul_date=new String[]{"ID","operation","date"};
        MyopenHelper myopenhelper = new MyopenHelper(Cultivateinfo_Activity.this, "seed.db", null, 1);
        SQLiteDatabase db = myopenhelper.getReadableDatabase();
        for(int i=1;i<=numble;i++){
            String string=String.valueOf(i);
            String times="第"+string+"次施肥时间";
            String[]args=new String[]{id,times};
            Cursor curor=db.query("cul_date",cul_date,"ID=? and operation=?",args,null,null,null,null);
            //String string=String.valueOf(i);
            String datetext="...";
            if(curor.moveToFirst()){
                datetext=curor.getString(2);
            }
            curor.close();
            addLinearLayout(linearLayout2,times,datetext);

        }
        db.close();
    }
}
